// pages/teacher/teacherCourse/teacherCourse.js
import {teacher_active} from "../../../../asserts/js/common/barList";
import {post} from "../../../../asserts/js/request";
import {initCourseInit, getCourseParam} from "../../../../asserts/js/common/shareFun";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barList: teacher_active(0),
    top: app.globalData.bottom + 'px',
    title: {
      title1: "舞蹈",
      title2: "课程类型"
    },
    alter_show: false,
    tabObj: {},
    list: [],
    classId: "",
    danceKind: [],
    courseLevel: [],
    courseNature: [],
    courseKind: [],
    allChoose: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var obj = initCourseInit(app.globalData.enum_courseTypes, app.globalData.enum_courseKinds, app.globalData.enum_courseNature)
    console.log(obj)
    this.setData({
      tabObj: obj,
      classId: options.classId
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectBoxShow: function(){
    this.setData({
      alter_show: true
    })
  },
  alterCancel: function() {
    this.setData({
      alter_show: false
    })
  },
  next: function() {
    var this_ = this;
    var activeList = []
    for(var item of this.data.list){
      if(item.active) {
        activeList.push({
          name: item.courseName,
          id: item.courseId
        })
      }
    }
    if(activeList.length == 0) {
      wx.showToast({
        title: '请选择课程',
        icon: "none"
      })
      return
    }

    var list = wx.getStorageSync('subList').map(item => {
      item.studentList = item.studentList.map(el => {
        if(el.active) {
          el.courseList = activeList
        }
        return el;
      })
      return item;
    });

    wx.setStorageSync('subList', list);
    console.log(list)
    wx.navigateTo({
      url: '/pages/teacher/myclass/teacherWorkSure/teacherWorkSure?classId=' + this_.data.classId
    })
  },
  back: function() {
    wx.navigateBack({
      delta: 0,
    })   
  },
  courseChange: function(e) {
    this.setData({
      ["list["+e.currentTarget.dataset.index+"].active"]: e.detail.value
    })
    if(e.detail.value) {
      var flag = true;
      for(var item of this.data.list) {
        if(!item.active){
          flag = false
        }
      }
      this.setData({
        allChoose: flag
      })
    }else {
      this.setData({
        allChoose: false
      })
    }
  },
  getData: function() {
    var this_ = this;
    post("queryCourse",{
      classId: this.data.classId,
      danceKind: this.data.danceKind.join("|"),
      courseKind: this.data.courseLevel.join("|"),
      courseNature: this.data.courseNature.join("|")
    }).then(data => {
      var tempList = data.courseInfo.map(item => {
        var arr = new Array();
        arr.push(item.courseNature);
        arr.push(item.danceKind);
        arr.push(item.danceKind);
        item.list = arr;
        item.active = false;
        return item;
      })
      this_.setData({
        list: tempList
      })
    })
  },
  selectSure: function(e) {
    let this_ = this;
    var paramList = getCourseParam(e.detail.tabList);
    this.setData({
      danceKind: paramList.danceKind,
      courseLevel: paramList.courseLevel,
      courseNature: paramList.courseNature,
      alter_show: false
    }, function(){
      this_.getData()
    })
  },
  checkAll: function() {
    let this_ = this;
    this_.setData({
      list: this_.data.list.map(item => {
        item.active = !this_.data.allChoose
        return item
      }),
      allChoose: !this_.data.allChoose
    })
  }
})